package com.health.mapp.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import com.health.db.model.UserAccountJournalExample;
import com.health.db.model.UserAccountJournalTemp;
import com.health.db.model.UserAccountLedger;
import com.health.db.service.user.IUserAccountJournalService;
import com.health.db.service.user.IUserAccountLedgerService;
import com.health.mapp.bean.Header;
import com.health.mapp.bean.QueryBillReq;
import com.health.mapp.bean.QueryBillResp;
import com.health.mapp.utils.Response;
import com.health.mapp.utils.ResultCode;

/**
 *  账单查询
 * @author HiRoad
 *
 */
public class QueryBillImpl implements ActionInterface {
	
	private IUserAccountLedgerService userAccountLedgerService ;
	private IUserAccountJournalService userAccountJournalService;
	@Override
	public Response execute(Header header, Object reqBean) throws Exception {
		
		QueryBillReq req=(QueryBillReq) reqBean;
		QueryBillResp _resp= new QueryBillResp();
		
		
		UserAccountJournalExample temp = new UserAccountJournalExample();
		Integer pageNo=req.getPageNo();
		Integer pageSize=req.getPageNum();
		if(null!=pageNo&&null!=pageSize){
			temp.setOffset((pageNo.intValue()-1)*pageSize.intValue());
			temp.setPageSize(pageSize.intValue());
		}
		
		if ((req.getUserId()==0&&req.getDocId()==0)||(req.getUserId()!=0&&req.getDocId()!=0)) {
			_resp.setErrorCode(ResultCode.OPERATION_LAKE_PARAMETER_ERROR);
			_resp.setErrorMsg(ResultCode.OPERATION_LAKE_PARAMETER_ERROR_NOTE);
			return _resp;
		}
		
		
		if(req.getUserId()!=0){
			temp.setUserId(req.getUserId());
		}else{
			temp.setUserId(req.getDocId());
		}
		
		List<UserAccountJournalTemp> billList = userAccountJournalService.selectUserAccountJournalSimple(temp);
		UserAccountLedger  userAccountLedger = userAccountLedgerService.selectByUserId(temp.getUserId());
		
		if (null!=userAccountLedger&&null!=userAccountLedger.getBalanceTotal()) {
			_resp.setBalance(String.valueOf(userAccountLedger.getBalanceTotal().setScale(2,  BigDecimal.ROUND_HALF_UP)));
		}
		
		List<UserAccountJournalTemp> billListreturn = new ArrayList<UserAccountJournalTemp>();
		
		//old
		/*for (int i = 0; i < billList.size(); i++) {
			UserAccountJournalTemp uajtemp = billList.get(i) ;
			
			Double total = Double.valueOf(uajtemp.getBillAmount());
			
			if (total>0) {
				
				if (uajtemp.getBillType().equals(String.valueOf(Constant.CONSTANTS_PAY_ACTION_ID_21.intValue()))
							||uajtemp.getBillType().equals(String.valueOf(Constant.CONSTANTS_PAY_ACTION_ID_23.intValue()))) {
				}else{
					String enterAccount = "+"+uajtemp.getBillAmount();
					uajtemp.setBillAmount(enterAccount);
				}
			}
			billListreturn.add(uajtemp);
		}*/
		//新
		//_resp.setBillList(billListreturn);
		//旧
		_resp.setBillList(billList);
		_resp.setPageNo(pageNo);
		_resp.setPageNum(pageSize);
		_resp.setErrorCode(ResultCode.OPERATION_SUCCESS);
		_resp.setErrorMsg(ResultCode.OPERATION_SUCCESS_NOTE);
 
		return _resp;
	}

	public IUserAccountLedgerService getUserAccountLedgerService() {
		return userAccountLedgerService;
	}

	public void setUserAccountLedgerService(
			IUserAccountLedgerService userAccountLedgerService) {
		this.userAccountLedgerService = userAccountLedgerService;
	}

	public IUserAccountJournalService getUserAccountJournalService() {
		return userAccountJournalService;
	}

	public void setUserAccountJournalService(
			IUserAccountJournalService userAccountJournalService) {
		this.userAccountJournalService = userAccountJournalService;
	}

	
	
}
